from wordcloud import WordCloud
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import jieba

# 1. 读取欢迎词文本
with open("welcome.txt", "r", encoding="utf-8") as f:
    text = f.read()

# 2. 中文分词（过滤停用词）
text_cut = " ".join(jieba.cut(text))
stopwords = {"的", "是", "和", "在", "了", "将"}  # 自定义停用词

# 3. 加载蒙版图片（可选）
mask = np.array(Image.open("mask.png"))  # 若无需形状，设为None

# 3. 生成词云
wordcloud = WordCloud(
    font_path="C:/Windows/Fonts/simhei.ttf",  # 中文字体路径
    width=1000,
    height=800,
    background_color="white",
    mask=mask,
    stopwords=stopwords,
    colormap="viridis",      # 颜色主题：viridis, plasma, magma
    max_words=50,            # 最大词数
    contour_width=2,         # 轮廓线宽度
    contour_color="purple"   # 轮廓颜色
).generate(text_cut)

# 5. 显示并保存
plt.figure(figsize=(12, 10))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.title("欢迎学习Python程序课程！", fontsize=20, color="blue")
plt.show()
wordcloud.to_file("welcome_cloud.png")  # 保存词云